package com.ykt.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;


public class Shop extends SeachModel<Shop>{

	private static final long serialVersionUID = 1L;
	
	public static final Shop dao = new Shop();
	
	public Shop findShopByName(String name){
		return dao.findFirst("select * from shop where name=?",name);
	}
	public ShopType getShopType(){
		return ShopType.dao.findById(getInt("type"));
	}
	//得到图片
	public List<ProductImg> getShopImgs(){
		String sql="select * from product_img where shop_id=?";
		return ProductImg.dao.find(sql, this.getInt("id"));
	}
	//删除所有图片
	public void deleteAllPic(Integer shop_id){
		if(shop_id!=null)
			Db.update("delete from product_img where shop_id=?", shop_id);
	}
	/**
	 * 得到所有关联子商户的类别
	 * @param 主商户id
	 * @return
	 */
	public List<ShopType> getSubShopTypes(Integer pid){
		
		String sql="select DISTINCT st.* from shop_relation sr"
				+ "inner join shop s"
				+ "on sr.sid = s.id"
				+ "inner join shop_type st"
				+ "on st.id = s.type"
				+ "where sr.pid=?";
		return ShopType.dao.find(sql, pid);
	}
	/**
	 * 得到所有当前商户 关联子商户的类别
	 * @return
	 */
	public List<ShopType> getSubShopTypes(){
		return getSubShopTypes( getInt("id"));
	}
}
